<?php

require_once 'smarty/smarty.class.php';

class BMode_view_smarty extends Smarty implements DIView
{

    /**
     * 模板文件后缀名
     *
     * @access private
     * @var string
     */
    private $_template_ext = '.tpl';

    /**
    * 模板左界定符
    *
    * @access private
    * @var string
    */
    private $_left_delimiter = '<!--{';

    /**
    * 模板右界定符
    *
    * @access private
    * @var string
    */
    private $_right_delimiter = '}-->';

    /**
    * 是否缓存
    *
    * @access private
    * @var bool
    */
    private $_caching = false;

    private $_file_name = '';

    /**
     * 缓存过期时间
     *
     * @access private
     * @var int
     */
    private $_cache_lifetime = 3600;

    /**
     * 模板文件存放目录名
     *
     * @var string
     */
    private $_view_dir = 'view';

	/**
	 * 模板插件存放目录
	 *
	 * @var array
	 */
	private $_plugins_dir = array('plugins');

    /**
     * 构造函数
     *
     * @access public
     * @return null
     */
    function __construct()
    {
        parent::Smarty();
		$this->_template_ext = '.tpl'; //模板文件后缀名
		$this->_left_delimiter = '<!--{'; //模板命令左界定符
		$this->_right_delimiter = '}-->';//模板命令右界定符
		$this->_caching = false; //是否缓存编译结果
		$this->_cache_lifetime = 30; //缓存过期时间
		$plugins_dir = array();//允许自定义的插件存放目录，数组或字符串
		$plugins_dir[0] == 'plugins' || array_unshift($plugins_dir, 'plugins');
		$this->_plugins_dir = $plugins_dir;

        $this->left_delimiter   = $this->_left_delimiter;
        $this->right_delimiter  = $this->_right_delimiter;
        $this->caching          = $this->_caching;
        $this->cache_lifetime   = $this->_cache_lifetime;
		$this->plugins_dir		= $this->_plugins_dir;
        $this->template_dir     = BLoader::PathName('>/templates/');
        $this->compile_dir      = BLoader::PathName('./data/cache/view/compile/');
        $this->cache_dir        = BLoader::PathName('./data/cache/view/cache/');
        $this->autoload_filters = array('output'=> array('manage'));//页面输出控制
    }

    /**
     * 重定义加载文件方式
     *
     * 允许在调用路径亲缘树内查找文件
     *
     * @access public
     * @param array $params
     */
    function _smarty_include($params)
    {
        $depth = $params['smarty_include_vars']['__depth__'];
        if ($depth == 'root'){
            $dirs = BLoader::PathName('./templates/');
        } else {
            $dirs = $this->template_dir;
        }
        $pathinfo = pathinfo($params['smarty_include_tpl_file']);
        //$params['smarty_include_tpl_file'] = $dirs . $params['smarty_include_tpl_file'];
        $params['smarty_include_tpl_file'] = $this->_checkTemplate($pathinfo['filename']);
        parent::_smarty_include($params);
    }

    /**
     * 添加变量解析
     *
     * @access public
     * @param string|array var
     * @param mixed val
     * @return object
     */
    function set($var, $val = null)
    {
        parent::assign($var, $val);
        return $this;
    }

    /**
     * 返回已添加的变量
     *
     * @access public
     * @param string $var
     * @return array
     */
    function get($var = null)
    {
        return is_null($var) ? parent::get_template_vars() : parent::get_template_vars($var);
    }


    /**
     * 校验模板名
     *
     * @access private
     * @return string
     */
    private function _checkTemplate($template)
    {
        if (!is_null($template)) {
            $tplname = $this->template_dir . $template;
        } else {
            $tplname = $this->template_dir . BConfig::Action();
        }
        $tplname.= $this->_template_ext;
        if (!is_file($tplname)) {
        	foreach (array_reverse(BLoader::familyTree()) as $v) {
        		if (is_file($v . BLoader::SPACE_DIR .'templates'. BLoader::SPACE_DIR .$template.$this->_template_ext)) {
        			return $v . BLoader::SPACE_DIR .'templates'. BLoader::SPACE_DIR .$template.$this->_template_ext;
        			break;
        		}
        	}
        }
        return $tplname;
    }

    /**
     * 解析模板并显示网页
     *
     * @access public
     * @param string $template  模板名称或地址
     * @param bool $absolute  模板地址是否绝对地址
     * @param bool $fetch 是否只返回字符串
     * @return null
     */
    function display($template = null, $fetch = false)
    {
        $tplname = $this->_checkTemplate($template);
        return $fetch ? parent::fetch($tplname) : parent::display($tplname);
    }

}

?>